const async = require('async');
const mysql = require('../../utils/db').mysql;
const qiniu = require('../../utils/qiniu');
exports.page = function(req, res, next){
    const id = req.query.id;
    const userInfo = req.session.userInfo;
    if(!id){
        res.status = 404;
        res.send('404');
        return;
    }
    if(userInfo.flowerAuthority != 1){
        res.status = 404;
        res.send('404');
        return;
    }
    const qiniuToken = qiniu .getToken();
    req.session.touch();
    async.auto({
        flower: function(callback){
            const queryStr = 'SELECT * FROM huayu WHERE id=' + id;
            mysql.getConnection(function(err, conn) {
                conn.query(queryStr, function (err, result) {
                    if (err) {
                        // 错误处理
                    }
                    if (result.length > 0) {
                        callback(null, result[0]);
                    } else {
                        callback(null, {});
                    }
                    mysql.releaseConnection(conn);
                })
            })
        }
    }, function(err, results){
        res.render('admin/updateFlower', {
            userInfo: userInfo,
            results: results,
            channel: {
                type: 'manage-huayu',
                index: 1
            },
            qiniuToken: qiniuToken
        });
    })
}
exports.updateFlower = function(req, res, next){
    const userInfo = req.session.userInfo;
    if(userInfo.flowerAuthority != 1){
        res.send({
            code: 400,
            msg: '更新失败'
        })
        return;
    }
    const id = req.body.id;
    const data = {
        title: req.body.title,
        thumb: req.body.thumb,
        desc: req.body.desc,
        content: req.body.content
    };
    const queryStr = 'UPDATE huayu SET title=?,thumb=?,hydesc=?,content=? WHERE id="' + id + '"';
    const valueArr = [data.title, data.thumb, data.desc, data.content];
    mysql.getConnection(function(err, conn) {
        conn.query(queryStr, valueArr, function (err, result) {
            if (err) {
                res.send({
                    code: 400,
                    msg: '更新失败'
                })
            } else {
                res.send({
                    code: 200,
                    msg: '更新成功'
                })
            }
            mysql.releaseConnection(conn);
        })
    })
}
exports.getFlowerInfo = function(req, res, next){
    const id = req.body.hyid;
    const queryStr = 'SELECT * FROM huayu WHERE id=' + id;
    mysql.getConnection(function(err, conn) {
        conn.query(queryStr, function (err, result) {
            if (err) {
                // 错误处理
                res.send({});
            }
            if (result.length > 0) {
                res.send(result[0]);
            } else {
                res.send({});
            }
            mysql.releaseConnection(conn);
        })
    })
}